<?php

namespace App\Http\Controllers\Supplier;
use App\Models\Distribution;
use App\Services\CommonService;
use Illuminate\Http\Request;

//配送方式
class DistributionWayController extends BaseController
{
    //配送方式列表
    public function distributionList(Request $request)
    {
        $input = $request->input();
        $other_admin = $this->users; //用户信息
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = Distribution::GetDistributionList($pageSize, array('d.member_id' => $other_admin['user_id'], 'd.is_delete' => Distribution::DISTRIBUTION_NOT_DELETE));
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['addtime'] = !empty($v['addtime']) ? dateFormat($v['addtime']) : '';
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //根据id查询配送方式 http://shop.cn/Supplier/DistributionWay/distributionById?id=
    public function distributionById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        $data = Distribution::GetDistributionById($input['id']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //添加或编辑配送方式
    public function addOrEditDistribution(Request $request)
    {
        $input = $request->input();
        $other_admin = $this->users; //用户信息
        if (empty($input['id'])) { //添加
            $addData = array(
                'member_id' => $other_admin['user_id'],
                'uid' => $other_admin['id'],
                'distribution' => $input['distribution'],
                'delivery' => $input['delivery'],
                'addtime' => time(),
            );
            $res = Distribution::addData($addData);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加失败');
            }
        } else { //编辑
            Distribution::editDataById($input['id'], $input);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
        }

    }

    //根据id删除配送方式
    public function delDistribution(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        Distribution::editDataById($input['id'], array('is_delete' => Distribution::DISTRIBUTION_IS_DELETE));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功');
    }


}
